import java.io.IOException;

/**
 * 
 * @author Minh-Tuan Ta
 * This class will catch the shut down hook and close the server socket and clean up
 * if necessary
 */
public class ServerShutDownThread extends Thread {
	SimpleWebServer sws;
	
	public ServerShutDownThread(SimpleWebServer sws)
	{
		this.sws = sws;
	}
	
	@Override
	public void run()
	{
		try
		{
			System.out.println("Shuting down server...");
			sws.setRunning(false);
			if (!sws.getServerSocket().isClosed())
			  sws.getServerSocket().close();
			System.out.println("Server stopped");
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			System.out.println("Shut down failed!!!!");
			e.printStackTrace();
		}
	}
}
